var oogfirefoxext = {
  onLoad: function() {
    // initialization code
    this.initialized = true;
    this.strings = document.getElementById("oogfirefoxext-strings");

    alert("Welcome to Oogy Extension");

  },
  onMenuItemCommand: function(e) {
    var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                                  .getService(Components.interfaces.nsIPromptService);
    promptService.alert(window, this.strings.getString("PrefMenuMessageTitle"),
                                this.strings.getString("PrefMenuMessage"));
  },
  TabAdded: function(){      
    oogfirefoxext.IterateAllBrowserTabs();
  },
  IterateAllBrowserTabs: function() {
    
    // We need elevated Security Permissions (also for our component)
    // netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        
    var strTitles = ""; // this variable will contain all the titles of all the tabs ';' seperated (first title is the selected tab)
    var strURIs = ""; // this variable will contain all the URIs of all the tabs ';' seperated (first URI is the selected tab)

    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                   .getService(Components.interfaces.nsIWindowMediator);                                                  
                                                                 
    var browserWin = wm.getMostRecentWindow("navigator:browser");   
    var tabbrowser = browserWin.getBrowser();

    // try getting the selected tab    
    var strActiveTabTitle = tabbrowser.contentDocument.title;
    var strActiveTabURI = tabbrowser.contentDocument.baseURI;
    
    // Add the Active Title/URI first
    strTitles += strActiveTabTitle;
    strURIs += strActiveTabURI;    
    
    //*debug*
    //alert(strTitles);
    //alert(strURIs);
    
    var strCurTabTitle;
    var strCurTabURI;
    
    // Check each tab of this browser instance
    var numTabs = tabbrowser.browsers.length;
   
    for(var index=0; index < numTabs; index++) 
    {      
      ////
      // fyi = https://developer.mozilla.org/en/XUL/browser
      ////
      var curBrowser = tabbrowser.getBrowserAtIndex(index);     
      strCurTabTitle = curBrowser.contentTitle;
      strCurTabURI = curBrowser.currentURI.spec;      
            
      // Active Tab is already added, so skip it
      if((strCurTabTitle == strActiveTabTitle) &&
         (strCurTabURI == strActiveTabURI))
      {
          continue;
      }
      else
      {
          // pass out all the titles and URIs
          strTitles += ("\n\t" + strCurTabTitle);
          strURIs += ("\n\t" + strCurTabURI);
      }        
    }
    
    //*debug*
    alert(strTitles);
    alert(strURIs);         
 },

};
window.addEventListener("load", function(e) { oogfirefoxext.onLoad(e); }, false);
window.addEventListener("TabOpen", function(e) { oogfirefoxext.IterateAllBrowserTabs(e); }, false);
window.addEventListener("TabMove", function(e) { oogfirefoxext.IterateAllBrowserTabs(e); }, false);
window.addEventListener("TabClose", function(e) { oogfirefoxext.IterateAllBrowserTabs(e); }, false);
window.addEventListener("TabSelect", function(e) { oogfirefoxext.IterateAllBrowserTabs(e); }, false);
//window.addEventListener("focus", function(e) { oogfirefoxext.IterateAllBrowserTabs(e); }, false);

//window.addEventListener("DOMActivate", function(e) { oogfirefoxext.IterateAllBrowserTabs(e); }, false);
//window.addEventListener("DOMSubtreeModified", function(e) { oogfirefoxext.IterateAllBrowserTabs(e); }, false);